<?php

/**
 * 商品控制器
 * @author 黄云鹏 <huangyunngo@foxmail.com>
 */
class Payback extends Web_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Order_model', 'order_model');
        $this->load->model('Notify_swiftpass_model');
        $this->load->model('Notify_swiftpass_alipay_model');
        $this->load->model('Orderdetail_model');
        $this->load->model('User_model');
        $this->load->helper('redis');
        $this->load->helper('sms');
    }


    // 微信公众号支付平台回调
    public function wxOpenBack()
    {
        $time_now = time();
        $postStr = $this->input->raw_input_stream;
        if (!empty($postStr)) {
            $xml = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); // 将xml字符串载入为对象，里面的值也是对象，需要强制转换
            $result_code = (string) $xml->result_code;
            $total_fee = (int) $xml->total_fee;
            $openid = (string) $xml->openid;//用户在商户appid下的唯一标识
            $time_end = (string) $xml->time_end;
            $out_trade_no = (string) $xml->out_trade_no;
            if ($result_code == 0) {
                $notify = $this->Notify_swiftpass_model->getNotifyByOrderId($out_trade_no);
                if (empty($notify)) {
                    $orderid_paytype = explode('_', $out_trade_no);
                    $order_id = $orderid_paytype[0];
                    $paytype = $orderid_paytype[1];
                    $setOrder = array('status' => 1, 'paytype' => $paytype, 'update_time' => $time_now);//订单状态：1=支付成功,2=已发货,3=未支付,4=超时,5=已退押金
                    $orderData = json_decode(getOrderFromRedisByOrderid($order_id));
                    if ($total_fee / 100 < $orderData->order->price) {
                        //参数错误
                        echo json_encode(array(
                            "result" => 2,
                        ));
                        return;
                    }
                    //更新订单状态，订单支付方式
                    $this->order_model->updateOrderByOrderid($order_id, $setOrder);
                    //清除该order的redis数据
                    delOrderFromRedisByOrderid($order_id);
                    //拆分订单详情
                    //先删除订单详情
                    $this->Orderdetail_model->delOrderDetailByOrderId($order_id);
                    foreach($orderData->order_detail as $key => $value)
                    {
                        //批量插入订单详情
                        $order_detail = array();
                        for($i = 0; $i < $value->amount ;$i++)
                        {
                            $order_detail[] = array('goods_id' => $value->goods_id,
                                                    'amount' => 1,
                                                    'activity_id' => $value->activity_id,
                                                    'price' => $value->price,
                                                    'order_id' => $value->order_id,
                                                    'update_time' => $time_now,
                                                    'last_return_time' => $time_now,
                                                    'status' => 1);
                        }
                        $this->Orderdetail_model->insertOrderDetailBatch($order_detail);
                    }
                    // file_put_contents('wxOpenBack_log.txt', json_encode($xml));
                    //插入威富通微信支付回调表数据
                    $data = array(
                        'bank_type' => (string) $xml->bank_type,
                        'mch_id' => (string) $xml->mch_id,
                        'fee_type' => (string) $xml->fee_type,
                        'is_subscribe' => (int) $xml->is_subscribe,
                        'nonce_str' => (string) $xml->nonce_str,
                        'openid' => (string) $xml->openid,
                        'out_trade_no' => (string) $xml->out_trade_no,
                        'out_transaction_id' => (string) $xml->out_transaction_id,
                        'pay_result' => (int) $xml->pay_result,
                        'result_code' => (string) $xml->result_code,
                        'sign' => (string) $xml->sign,
                        'sign_type' => (string) $xml->sign_type,
                        'status' => (string) $xml->status,
                        'time_end' => (string) $xml->time_end,
                        'total_fee' => (int) $xml->total_fee,
                        'trade_type' => (string) $xml->trade_type,
                        'transaction_id' => (string) $xml->transaction_id,
                        'version' => (string) $xml->version,
                    );
                    $this->Notify_swiftpass_model->addNotifyByData($data);
                    $user = $this->User_model->getUserByUid($orderData->order->uid);
                    sms_juhe(2, $user['mobile'], array('content' => $orderData->order->order_id));
                }
                echo "SUCCESS";
            }
        }
    }

    // 支付宝支付回调
    public function alipayBack()
    {
        $time_now = time();
        $postStr = $this->input->raw_input_stream;
        if (!empty($postStr)) {
            $xml = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); // 将xml字符串载入为对象，里面的值也是对象，需要强制转换
            $result_code = (string) $xml->result_code;
            $total_fee = (int) $xml->total_fee;
            $openid = (string) $xml->openid;//用户在商户appid下的唯一标识
            $time_end = (string) $xml->time_end;
            $out_trade_no = (string) $xml->out_trade_no;
            if ($result_code == 0) {
                $notify = $this->Notify_swiftpass_alipay_model->getNotifyByOrderId($out_trade_no);
                if (empty($notify)) {
                    $orderid_paytype = explode('_', $out_trade_no);
                    $order_id = $orderid_paytype[0];
                    $paytype = $orderid_paytype[1];
                    $setOrder = array('status' => 1, 'paytype' => $paytype, 'update_time' => $time_now);//订单状态：1=支付成功,2=已发货,3=未支付,4=超时,5=已退押金
                    $orderData = json_decode(getOrderFromRedisByOrderid($order_id));
                    if ($total_fee / 100 < $orderData->order->price) {
                        //参数错误
                        echo json_encode(array(
                            "result" => 2,
                        ));
                        return;
                    }
                    //更新订单状态，订单支付方式
                    $this->order_model->updateOrderByOrderid($order_id, $setOrder);
                    //清除该order的redis数据
                    delOrderFromRedisByOrderid($order_id);
                    //拆分订单详情
                    //先删除订单详情
                    $this->Orderdetail_model->delOrderDetailByOrderId($order_id);
                    foreach($orderData->order_detail as $key => $value)
                    {
                        //批量插入订单详情
                        $order_detail = array();
                        for($i = 0; $i < $value->amount ;$i++)
                        {
                            $order_detail[] = array('goods_id' => $value->goods_id,
                                                    'amount' => 1,
                                                    'activity_id' => $value->activity_id,
                                                    'price' => $value->price,
                                                    'order_id' => $value->order_id,
                                                    'update_time' => $time_now,
                                                    'status' => 1);
                        }
                        $this->Orderdetail_model->insertOrderDetailBatch($order_detail);
                    }
                    // file_put_contents('alipayBack_log.txt', json_encode($xml));
                    //插入威富通微信支付回调表数据
                    $data = array(
                        'mch_id' => (string) $xml->mch_id,
                        'nonce_str' => (string) $xml->nonce_str,
                        'openid' => (string) $xml->openid,
                        'out_trade_no' => (string) $xml->out_trade_no,
                        'out_transaction_id' => (string) $xml->out_transaction_id,
                        'pay_result' => (int) $xml->pay_result,
                        'result_code' => (string) $xml->result_code,
                        'sign' => (string) $xml->sign,
                        'status' => (string) $xml->status,
                        'time_end' => (string) $xml->time_end,
                        'total_fee' => (int) $xml->total_fee,
                        'trade_type' => (string) $xml->trade_type,
                        'transaction_id' => (string) $xml->transaction_id,
                        'version' => (string) $xml->version,
                        'charset' => (string) $xml->charset,
                    );
                    $this->Notify_swiftpass_alipay_model->addNotifyByData($data);
                    $user = $this->User_model->getUserByUid($orderData->order->uid);
                    sms_juhe(2, $user['mobile'], array('content' => $orderData->order->order_id));
                }
                echo "SUCCESS";
            }
        }
    }

}
